package lib_model.Transportion;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;

import lib_dal.Connection.ConnectMSSQL;
import lib_dal.Interface.ISql;
import lib_model.Orther.ReturnObj;

public class Transportion_history implements ISql {

	public int id;
	public int transportion_bill_id_ref;
	public String description;
	public Date date_create;

	public Transportion_history() {
	}

	@Override
	public ReturnObj Select() {
		String sql = "select id, transportion_bill_id_ref, description, date_create from dbo.transportion_history";
		ReturnObj obj = new ReturnObj();
		ArrayList<Transportion_history> arr = new ArrayList<Transportion_history>();
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			ResultSet rs = preState.executeQuery();
			while (rs.next()) {
				Transportion_history a = new Transportion_history();
				a.id = rs.getInt("id");
				a.transportion_bill_id_ref = rs
						.getInt("transportion_bill_id_ref");
				a.description = rs.getString("description");
				a.date_create = rs.getDate("date_create");
				arr.add(a);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		obj.ObjResult = arr;
		return obj;
	}

	@Override
	public ReturnObj Add() {
		String sql = "insert into dbo.transportion_history  (id,   transportion_bill_id_ref,   description,   date_create) values   (?,   ?,   ?,   GETDATE())";
		ReturnObj obj = new ReturnObj();
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			preState.setInt(1, this.id);
			preState.setInt(2, this.transportion_bill_id_ref);
			preState.setString(3, this.description);
			obj.Result = preState.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return obj;
	}

	@Override
	public ReturnObj Update() {
		String sql = "update dbo.transportion_history set transportion_bill_id_ref = ?,   description = ?,   date_create = ? where  id = ?";
		ReturnObj obj = new ReturnObj();
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			preState.setInt(1, this.transportion_bill_id_ref);
			preState.setString(2, this.description);
			preState.setDate(3, (java.sql.Date) this.date_create);
			preState.setInt(4, this.id);
			obj.Result = (preState.executeUpdate() != 0) ? true : false;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return obj;
	}

	@Override
	public ReturnObj Delete() {
		String sql = "delete from dbo.transportion_history where id = ?";
		ReturnObj obj = new ReturnObj();
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			preState.setInt(1, this.id);
			obj.Result = preState.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return obj;
	}

	@Override
	public ReturnObj GetInformation() {
		ReturnObj obj = new ReturnObj();
		String sql = "select id, transportion_bill_id_ref, description, date_create from dbo.transportion_history where id = ?";
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			preState.setInt(1, this.id);
			ResultSet rs = preState.executeQuery();
			while (rs.next()) {
				Transportion_history a = new Transportion_history();
				a.id = rs.getInt("id");
				a.transportion_bill_id_ref = rs
						.getInt("transportion_bill_id_ref");
				a.description = rs.getString("description");
				a.date_create = rs.getDate("date_create");
				obj.ObjResult = a;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return obj;
	}
}
